package com.jie.other.compose.study02

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jie.other.R
import com.jie.other.compose.data.SampleData

/**
 * 网格子项
 */
@Composable
fun FavoriteItem(
    @DrawableRes drawable: Int,
    @StringRes text : Int,
    modifier: Modifier = Modifier,
) {
    Surface(
        modifier = modifier,
        shape = MaterialTheme.shapes.small,
    ) {
        Row(
            modifier = Modifier
                .width(192.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Image(
                painter = painterResource(id = drawable),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(56.dp),
            )
            Text(
                text = stringResource(id = text),
                style = MaterialTheme.typography.body2,
                modifier = Modifier.padding(10.dp)
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewFavoriteItem() {
    FavoriteItem(
        drawable = R.drawable.profile_picture,
        text = R.string.other_favorite,
        modifier = Modifier.padding(8.dp)
    )
}

/**
 * 网格
 */
@Preview(showBackground = true)
@Composable
fun FavoriteGrid(
    modifier: Modifier = Modifier
) {
    LazyHorizontalGrid(
        rows = GridCells.Fixed(2),
        modifier = modifier.height(120.dp),
        contentPadding = PaddingValues(horizontal = 16.dp),
        horizontalArrangement = Arrangement.spacedBy(8.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp)
    ) {
        items(SampleData.favoriteSample) {
            FavoriteItem(
                drawable = it.drawable,
                text = it.text,
                modifier = Modifier.height(56.dp)
            )
        }
    }
}